package dao;

import dao.base.BaseDao;
import model.Article;
import model.Favorite;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.List;
import java.util.Map;

public class FavoriteDao extends BaseDao<Favorite> {
    private JdbcTemplate jdbcTemplate;

    public FavoriteDao(Class aClass) {
        super(Favorite.class);
    }
    public Integer findByFavoriteCountByArticleId(Integer artId) {
        try {
            String sql = "select count(1) from favorite where a_id=?";
            jdbcTemplate = getJdbcTemplate();
            return jdbcTemplate.queryForObject(sql,Integer.class,artId);
        } catch (Exception e) {
            return 0;
        }
    }

    public List<Map<String, Object>> findByUserFavorite(Integer id, Integer artId) {
        try{
            String sql = "select u.id,u.real_name,u.username " +
                    "from userfocus uf " +
                    "left join user u " +
                    "on u.id=uf.user_focus_id " +
                    "left join favorite f " +
                    "on f.u_id=u.id " +
                    "where uf.user_id=? and f.a_id=?";
            jdbcTemplate = getJdbcTemplate();
            return jdbcTemplate.queryForList(sql, id, artId);
        }catch (Exception e){
            return null;
        }
    }

    public Integer unFavorite(Integer id, Integer artId) {
        try{
            jdbcTemplate = getJdbcTemplate();
            return jdbcTemplate.update("delete from favorite where u_id=? and a_id=?", id, artId);
        }catch (Exception e){
            return 0;
        }
    }

    public Integer favorite(Integer id, Integer artId) {
        try{
            jdbcTemplate = getJdbcTemplate();
            return jdbcTemplate.update("insert into favorite(u_id,a_id) values(?,?)", id, artId);
        }catch (Exception e){
            return 0;
        }
    }

    public List<Article> favoritePage(Integer id, String title, Integer startIndex, Integer pageSize) {
        try{
            jdbcTemplate = getJdbcTemplate();
            String sql = "SELECT a.* " +
                    "FROM favorite f " +
                    "LEFT JOIN article a " +
                    "ON f.a_id = a.id " +
                    "WHERE f.u_id = ? " +
                    "AND title LIKE ? " +
                    "ORDER BY publish_date " +
                    "DESC LIMIT ?,?";
            return jdbcTemplate.query(sql,new BeanPropertyRowMapper<>(Article.class),id,"%"+title+"%",startIndex,pageSize);
        }catch (Exception e){
            return null;
        }
    }

    public Integer countByFavorite(Integer id, String title) {
        String sql = "select count(1) from favorite f left join article a on" +
                " f.a_id=a.id where f.u_id=? and title like ?";
        try{
            jdbcTemplate = getJdbcTemplate();
            return jdbcTemplate.queryForObject(sql,Integer.class,id,"%"+title+"%");
        }catch (Exception e){
            return 0;
        }
    }
}
